package tt.dz.order.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

/**
 * @Title XML工具类
 * @author zhoutian   
 * @date 2015-12-16
 */
public class XmlUtil {
	/**
	 * xml转Map
	 */
	@SuppressWarnings({ "rawtypes" })
    public static Map<String, Object> Xml2Map(String xmlStr) throws DocumentException{
		Document doc = DocumentHelper.parseText(xmlStr);
        Map<String, Object> map = new HashMap<String, Object>(); 
        if(doc == null) 
            return map; 
        Element root = doc.getRootElement(); 
        for (Iterator iterator = root.elementIterator(); iterator.hasNext();) { 
            Element e = (Element) iterator.next(); 
            List list = e.elements(); 
            if(list.size() > 0){ 
                map.put(e.getName(), Dom2Map(e)); 
            }else 
                map.put(e.getName(), e.getText()); 
        } 
        return map; 
    }
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
    private static Map Dom2Map(Element e){ 
        Map map = new HashMap(); 
        List list = e.elements(); 
        if(list.size() > 0){ 
            for (int i = 0;i < list.size(); i++) { 
                Element iter = (Element) list.get(i); 
                List mapList = new ArrayList(); 
                 
                if(iter.elements().size() > 0){ 
                    Map m = Dom2Map(iter); 
                    if(map.get(iter.getName()) != null){ 
                        Object obj = map.get(iter.getName()); 
                        if(!obj.getClass().getName().equals("java.util.ArrayList")){ 
                            mapList = new ArrayList(); 
                            mapList.add(obj); 
                            mapList.add(m); 
                        } 
                        if(obj.getClass().getName().equals("java.util.ArrayList")){ 
                            mapList = (List) obj; 
                            mapList.add(m); 
                        } 
                        map.put(iter.getName(), mapList); 
                    }else 
                        map.put(iter.getName(), m); 
                } 
                else{ 
                    if(map.get(iter.getName()) != null){ 
                        Object obj = map.get(iter.getName()); 
                        if(!obj.getClass().getName().equals("java.util.ArrayList")){ 
                            mapList = new ArrayList(); 
                            mapList.add(obj); 
                            mapList.add(iter.getText()); 
                        } 
                        if(obj.getClass().getName().equals("java.util.ArrayList")){ 
                            mapList = (List) obj; 
                            mapList.add(iter.getText()); 
                        } 
                        map.put(iter.getName(), mapList); 
                    }else 
                        map.put(iter.getName(), iter.getText()); 
                } 
            } 
        }else 
            map.put(e.getName(), e.getText()); 
        return map; 
    }
	
	/**
	 * Map转Xml
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static String map2Xml(Map<String, Object> map){
		StringBuilder xmlStr = new StringBuilder();
		xmlStr.append("<xml>");
		if(map != null && map.size() > 0){
			for(Map.Entry<String, Object> entry : map.entrySet()){
				String key = entry.getKey();
				Object value = entry.getValue();
				if (null == value) value = "";
				if (value.getClass().getName().equals("java.util.ArrayList")) {
					List list = (List) map.get(key);
					xmlStr.append("<" + key + ">");
					for (int i = 0; i < list.size(); i++) {
						Map hm = (Map) list.get(i);
						xmlStr.append(map2Xml(hm));
					}
					xmlStr.append("</" + key + ">");
				} else {
					if (value instanceof Map) {
						xmlStr.append("<" + key + ">");
						xmlStr.append(map2Xml((Map) value));
						xmlStr.append("</" + key + ">");
					} else {
						xmlStr.append("<" + key + ">" + value + "</" + key + ">");
					}
				}
			}
		}xmlStr.append("</xml>");
		return xmlStr.toString();
	}
	
	public static void main(String[] args) throws Exception {
		String xmlStr = "<xml><return_code><![CDATA[SUCCESS]]></return_code><a><b><c>111</c></b></a><return_msg><![CDATA[OK]]></return_msg></xml>";
		Map<String, Object> map = Xml2Map(xmlStr);
		System.out.println(map.toString());
	}
} 
